package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.MakeDTO;
import com.fact.dto.MarketDTO;
import com.fact.models.MarketService;
import com.fact.utils.AppConstant;

public class Market extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1881624620546983622L;
	private Map<String,Object> session;
	private ArrayList<MarketDTO> marketList;
	
	private String page=AppConstant.MARKET;
	private String pageTitle="FMtool - Market List";

	MarketDTO market=new MarketDTO();
	private String opType;
	MarketService marketService=new MarketService();
	
	public String marketList()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		marketList= marketService.getMarketList();
		
		return SUCCESS;
	}
	public String newMarketForm()
	{		
		return SUCCESS;
	}
	public String saveMarket()
	{ 
		
		if(marketService.getMarket(market.getMarketName())!=null)
			addActionMessage("Market Already Exist.");
		
		if(hasActionMessages())
			return  INPUT;


		if(marketService.saveMarket(market)==false)
				{
					addActionMessage("Error in Make Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", market.getMarketName()+" has successfully been added in Market List.");
					return  SUCCESS;
				}

	}
	
	public String editMarket()
	{
		this.session.put("operation_message", null);
		MarketDTO marketTmp=marketService.getMarket(market.getMarketName());
		boolean editFlag=false;
		if(marketTmp==null)
		{
			editFlag=true;
		}
		else
		{
			if(marketTmp.getMarketId()!=market.getMarketId())
			{
				editFlag=false;
			}
			else
			{
				editFlag=true;
			}
		}
		if(editFlag==true)
		{
			if(marketService.editMarket(market)==false)
			{
				addActionMessage("Error in Make Edit Operation.");
				return INPUT;
				
			}
			else
			{
				this.session.put("operation_message","Market with Market Name '"+ market.getMarketName()+"' has successfully been updated.");
				return  SUCCESS;
			}
		}
		else
		{

			addActionMessage("Update Falied. "+market.getMarketName()+" is already in the System.");
			return INPUT;
		}
	
	}
	public String deleteMarket()
	{
		if(marketService.deleteMarket(market.getMarketId())==false)
		{
			addActionMessage("Error in Market Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Market with Market Name '"+ market.getMarketName()+"' has successfully been deleted from Market List.");
			return  SUCCESS;
		}
	
	}
	
	public ArrayList<MarketDTO> getMarketList() {
		return marketList;
	}

	public void setMarketList(ArrayList<MarketDTO> marketList) {
		this.marketList = marketList;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public MarketDTO getMarket() {
		return market;
	}

	public void setMarket(MarketDTO market) {
		this.market = market;
	}

	public String getOpType() {
		return opType;
	}

	public void setOpType(String opType) {
		this.opType = opType;
	}

	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
}
